import numpy as np

def optimizeArgs(data, alpha=0.5, iters=500):
    """
        优化参数w b, alpha为优化的步长, iters为迭代次数
    """
    w, b = np.array([0, 0]), 0
    for i in range(iters):
        j = 1
        for row in data:
            flag = (b + np.dot(w, row[0:2])) * row[2]
            if flag > 0:
                j += 1
                #  print('j: ', j, ' flag: ', flag)
                continue
            else:
                j = 0
            w = w + alpha * row[0:2] * row[2]
            b = b + alpha * row[2]

            print('row: ', row, 'w: ', w, ' b: ', b, ' j: ', j, ' flag: ', flag)

        if j == len(data):
            # 迭代控制
            break
    return w, b


if __name__ == '__main__':
    data = np.array([
        [3, 3, 1],
        [4, 3, 1],
        [1, 1, -1]
    ])
    w_min, b_min = optimizeArgs(data)
    print(w_min, b_min)

